package cn.study.interruptTest;

/**
 * @Author     ：跟大佳一起学架构
 * @Description：如何安全中断线程
 */
public class EndThread {
	private static class UseThread extends Thread{
		public UseThread(String name) {
			super(name);
		}
		@Override
		public void run() {
			String threadName = Thread.currentThread().getName();
			while(!isInterrupted()){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					System.out.println(threadName+"inner InterruptedException flag ="+isInterrupted());
					// 捕获中断异常后，会将中断标志置为未中断，所以需要再次中断后才可以退出循环
					// TODO: 处理安全退出逻辑
//					interrupt();
				}
				System.out.println(threadName+"inner interrrupt flag ="+isInterrupted());
			}
			System.out.println("跳出循环："+isInterrupted());
		}
	}
	public static void main(String[] args) throws InterruptedException {
		UseThread endThread = new UseThread("endThread");
		endThread.start();
		Thread.sleep(4000);
		endThread.interrupt();//中断线程，其实设置线程的中断标识位=true
	}

}
